/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.terminalapplicationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.terminalapplicationmodule.data.TerminalApplicationModuleDataObject;
import org.testis.utils.TerminalApplicationExecutor;
import java.io.IOException;


/**
 * Defines an action that does execution of a terminall application
 * @author nurhat
 */
public class ExecuteAction extends AtofModuleAction
{

    @Override
    public boolean operateRequest(AtofModuleService request)
    {

        try {
            TerminalApplicationModuleDataObject dataObj =
                (TerminalApplicationModuleDataObject) request.getDataObj();
            LogSenderService.putMessageOnQueue(
                "TerminalAppExecuteAction",
                AtofLogLevel.INFO,
                "Starting to execute terminal application....With data:" +
                dataObj.toString(),
                dataObj.getMessageId());
            TerminalApplicationExecutor executor =
                new TerminalApplicationExecutor(dataObj.getAppFullPath(),
                                                dataObj.getParams());
            Integer exitVal = executor.runExec();

            dataObj.setOutputString(executor.getOutputString());
            dataObj.setErrorString(executor.getErrorString());
            dataObj.setExitCode(exitVal.toString());
            LogSenderService.putMessageOnQueue(
                "TerminalAppExecuteAction",
                AtofLogLevel.INFO,
                "Terminal application  executed....With data:" +
                dataObj.toString(),
                dataObj.getMessageId());
            return true;
        } catch (IOException ex) {
            throw new TestStepException(TestStepResultType.MISTAKE,
                                        "Execution of Terminal Application failed..." +
                                        ex.getMessage());

        } catch (Exception ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                                        "Execution of Terminal Application failed..." +
                                        ex.getMessage());
        }
    }
}